package com.itheima.controller;


import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.po.CheckitemPo;
import com.itheima.service.CheckitemService;
import com.itheima.task.RedisTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
@Api(tags = "检查项管理")
public class CheckitemController {

    @Autowired
    private CheckitemService checkitemService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("新增检查项功能")
    @PostMapping("/add")
    public Result backendAdd(@RequestBody CheckitemPo checkitem) {
        checkitemService.add(checkitem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    @ApiOperation("检查项修改接口")
    @PostMapping("/edit")
    public Result editCheckItem(@RequestBody CheckitemPo checkitem) {
        checkitemService.editCheckItem(checkitem);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @ApiOperation("检查项分页查询接口")
    @PostMapping("/findPage")
    public PageResult Page(@RequestBody QueryPageBean queryPageBean) {
        PageResult rows = checkitemService.pageQuery(queryPageBean);
        return rows;
    }

    @ApiOperation("检查项删除接口")
    @GetMapping
    public Result delete(Integer id) {
        checkitemService.deleteByID(id);
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @ApiOperation("查询所有检查项")
    @GetMapping("/findAll")
    public Result findAll(){


        String key="Setmeal_"+"001";
        List<CheckItem> list = (List<CheckItem>) redisTemplate.opsForValue().get(key);
        if(list==null || list.size()==0){
            log.info("缓存中暂无此列表数据");
            list = checkitemService.findAllCheckItem();
            redisTemplate.opsForValue().set(key,list);
        }
//        list=checkitemService.findAllCheckItem();
//        List<CheckItem> list=checkitemService.findAllCheckItem();
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }

    @ApiOperation("根据id查询对应检查项")
    @GetMapping("/findById")
    public Result findById(Integer id){
        CheckItem checkItem=checkitemService.findById(id);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
    }
    @ApiOperation("根据检查组id查询对应检查项")
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer groupId){
        List<Integer> list=checkitemService.getGroupId(groupId);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }
}
